package com.group7.mall.service.impl;

import com.group7.mall.dao.TbMessageDao;
import com.group7.mall.dao.TbUserDao;
import com.group7.mall.req.MessagePageRequest;
import com.group7.mall.dto.MessagePreviewDto;
import com.group7.mall.entity.TbMessage;
import com.group7.mall.entity.TbUser;
import com.group7.mall.service.MessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private TbMessageDao tbMessageDao;

    @Resource
    private TbUserDao tbUserDao;

    @Override
    public Integer messageToRead(TbMessage tbMessage) {
        return tbMessageDao.messageToRead(tbMessage);
    }

    @Override
    public List<TbMessage> queryMessageByTwoUser(MessagePageRequest messagePageRequest) {
        List<TbMessage> tbMessageList = tbMessageDao.selectChatRecords(messagePageRequest);
        Collections.reverse(tbMessageList);
        for (TbMessage x: tbMessageList) {
            if (x.getFromUserId().equals(messagePageRequest.getToUserId())
                    && x.getToUserId().equals(messagePageRequest.getFromUserId())){
                tbMessageDao.updateReadStatus(x.getMessageId());
            }
        }
        List<TbMessage> tbMessageList1 = tbMessageDao.selectChatRecords(messagePageRequest);
        Collections.reverse(tbMessageList1);
        return tbMessageList1;
    }

    @Override
    public int clearMessageInDatabase() {
        return tbMessageDao.clearMessageInDatabase();
    }

    @Override
    public List<MessagePreviewDto> queryLatestMessagesByUserId(String userOpenId) {
//        List<TbMessage> tbMessageList = tbMessageDao.queryLatestMessagesByUserId(tbUser.getUserOpenId());
        List<String> tbMessageList = tbMessageDao.queryMessageUser(userOpenId);
        List<MessagePreviewDto> messagePreviewDtoList = new ArrayList<>();
        for(String x: tbMessageList) {
//            System.out.println(x);
            TbMessage tbMessage = tbMessageDao.findLatestChatRecordBetweenUsers(userOpenId, x);
            String userOpenId2;
            MessagePreviewDto messagePreviewDto = new MessagePreviewDto();
            if (!Objects.equals(tbMessage.getFromUserId(), userOpenId)) {
                userOpenId2 = tbMessage.getFromUserId();
            } else {
                userOpenId2 = tbMessage.getToUserId();
            }
            TbUser tbUser1 = tbUserDao.queryByUserOpenId(userOpenId2);
            TbMessage tbMessage1 = new TbMessage();
            tbMessage1.setFromUserId(userOpenId2);
            tbMessage1.setToUserId(userOpenId);
            messagePreviewDto.setUnreadNumber(tbMessageDao.selectUnreadRecordsCount(tbMessage1));
            messagePreviewDto.setUserName(tbUser1.getUsername());
            messagePreviewDto.setUserOpenId(userOpenId2);
            messagePreviewDto.setUserImage(tbUser1.getUserImage());
            messagePreviewDto.setTimeStamp(tbMessage.getTimeStamp());
            if (tbMessage.getMessageType() == 1) {
                messagePreviewDto.setMessageType(1);
                messagePreviewDto.setMessageContent("[图片]");
            } else {
                messagePreviewDto.setMessageType(0);
                messagePreviewDto.setMessageContent(tbMessage.getMessage());
            }
            messagePreviewDtoList.add(messagePreviewDto);
//            System.out.println(tbMessageDao.findLatestChatRecordBetweenUsers(tbUser.getUserOpenId(), x));
        }

//        tbMessageList.forEach(tbMessage -> {
//            String userOpenId;
//            MessagePreviewDto messagePreviewDto = new MessagePreviewDto();
//            if (!Objects.equals(tbMessage.getFromUserId(), tbUser.getUserOpenId())) {
//                userOpenId = tbMessage.getFromUserId();
//            } else {
//                userOpenId = tbMessage.getToUserId();
//            }
//            TbUser tbUser1 = tbUserDao.queryByUserOpenId(userOpenId);
//            TbMessage tbMessage1 = new TbMessage();
//            tbMessage1.setFromUserId(userOpenId);
//            tbMessage1.setToUserId(tbUser.getUserOpenId());
//            messagePreviewDto.setUnreadNumber(tbMessageDao.selectUnreadRecordsCount(tbMessage1));
//            messagePreviewDto.setUserName(tbUser1.getUsername());
//            messagePreviewDto.setUserOpenId(userOpenId);
//            messagePreviewDto.setUserImage(tbUser1.getUserImage());
//            messagePreviewDto.setTimeStamp(tbMessage.getTimeStamp());
//            if (tbMessage.getMessageType() == 1) {
//                messagePreviewDto.setMessageType(1);
//                messagePreviewDto.setMessageContent("[图片]");
//            } else {
//                messagePreviewDto.setMessageType(0);
//                messagePreviewDto.setMessageContent(tbMessage.getMessage());
//            }
//            messagePreviewDtoList.add(messagePreviewDto);
//        });
        return messagePreviewDtoList;
//        return null;
    }

    @Override
    public Integer getAllUnreadMessage(TbUser tbUser) {
        return tbMessageDao.countUnreadMessage(tbUser.getUserOpenId());
    }
}
